package HttpBean.Response;

import HttpBean.Request.Request;

/**
 * 使用原型模式，创建各类Response对象
 */
public class Response implements Cloneable{
    private ResponseLine line;
    private ResponseHeader header;
    private ResponseEntity entity;
    public Response() {
        this.line = new ResponseLine();
        this.header = new ResponseHeader();
        this.entity = new ResponseEntity();
    }
    public void setLineHttpVersion(String httpVersion) {
        this.line.setHttpVersion(httpVersion);
    }
    public void setLineStatus(String status){
        this.line.setStatus(status);
    }
    public void setHeader(String key,String value) {
        this.header.set(key,value);
    }
    public void setEntity(String entity){
        this.entity.setEntity(entity);
    }
    public String getResponse(){
        String[] headers = this.header.getHeader();
        String str = "";
        str += this.line.getLine()+"\r\n";
        for(int i=0;i<headers.length;i++) str += headers[i] + "\r\n";
        str+="\r\n";
        str += this.entity.getEntity()+"\r\n";
        return str;
    }
    public void initResponse(Request request){
        setLineHttpVersion(request.getRequestLine().getHttpVersion());
        setHeader("Server", "Server: Pomcat/1.0.0");
        setHeader("Content-Type", "text/html; charset=utf-8");
        setHeader("Access-Control-Allow-Headers", "x-requested-with,Authorization");
    }
    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}
